{% extends "projects/project_edit_base.html" %}

{% load i18n %}

{% block title %}{% trans "Integration" %}{% endblock %}

{% block nav-dashboard %} class="active"{% endblock %}

{% block editing-option-edit-integrations %}class="active"{% endblock %}

{% block project-integrations-active %}active{% endblock %}
{% block project_edit_content_header %}
  {% blocktrans trimmed with type=integration.get_integration_type_display %}
    Integration - {{ type }}
  {% endblocktrans %}
{% endblock %}

{% block project_edit_content %}
  {% if integration.has_sync %}
    {% if integration.can_sync %}
      <p>
        {% blocktrans trimmed %}
          This integration is being managed automatically by Read the Docs. If
          it isn't functioning correctly, try re-syncing the webhook:
        {% endblocktrans %}
      </p>
    {% else %}
      {% comment %}
        Display information for manual webhook set up if either case is true:

        * Integration doesn't have the ability to sync
        * Integration has ability to sync, but we don't have the data returned
          from the provider API on webhook creation (or webhook was automatically
          created on new data from an old webhook)
      {% endcomment %}
      <p>
        {% blocktrans trimmed %}
          This integration is not managed by Read the Docs currently.
          If this integration is not functioning correctly,
          you can try re-syncing it. Otherwise you'll need to update
          the configuration on your repository.
          You can use the following address to manually configure this webhook.
        {% endblocktrans %}
      </p>
    {% endif %}
  {% endif %}

  <p>
    {% blocktrans trimmed %}
      To manually configure this webhook with your provider, use the
      following information:
    {% endblocktrans %}
  </p>

  <div class="integration-details">
    <p>
      <p>
        <label for="id_webhook_url">{% trans "Webhook URL:" %}</label>
        {% url 'api_webhook' project_slug=project.slug integration_pk=integration.pk as webhook_url %}
        <input type="text" id="id_webhook_url" value="https://{{ PRODUCTION_DOMAIN }}{{ webhook_url }}">
      </p>
    </p>

    {% if not integration.has_sync %}
      {# For generic webhooks #}
      <p>
      <label for="id_secret">{% trans "Secret:" %}</label>
        <input type="text" id="id_secret" value="{{ integration.token }}">
      </p>
    {% elif integration.secret %}
      <p>
        <label for="id_secret">{% trans "Secret:" %}</label>
        <input type="text" id="id_secret" value="{{ integration.secret }}">
      </p>
    {% endif %}
  </div>

  {% if integration.has_sync and not integration.secret %}
  <p class="empty">
    {% blocktrans trimmed %}
    This integration does not have a secret,
    the authenticity of the incoming webhook cannot be verified.
    <strong>Click on "Resync webhook" to generate a secret.</strong>
    Read more in our <a href="https://blog.readthedocs.com/security-update-on-incoming-webhooks/">blog post</a>.
    {% endblocktrans %}
  </p>
  {% endif %}

  {% if not integration.can_sync %}
    <p>
      {% blocktrans trimmed %}
        For more information on manually configuring a webhook, refer to
        <a href="https://docs.readthedocs.io/page/guides/setup/git-repo-automatic.html">
          our webhook documentation.
        </a>
      {% endblocktrans %}
    </p>
  {% endif %}

  {% if integration.has_sync %}
    <form method="post" action="{% url 'projects_integrations_webhooks_sync' project_slug=project.slug integration_pk=integration.pk %}">
      {% csrf_token %}
      <input type="submit" value="{% trans "Resync webhook" %}">
    </form>
  {% endif %}

  <h3>{% trans "Recent Activity" %}</h3>

  <div class="module-list-wrapper httpexchanges">
    <ul>
      {% for exchange in integration.exchanges.all %}
        <li class="module-item">
          <span class="status status-{% if exchange.failed %}fail{% else %}pass{% endif %}">{{ exchange.status_code }}</span>
          <a href="{% url 'projects_integrations_exchanges_detail' project_slug=project.slug integration_pk=integration.pk exchange_pk=exchange.pk %}">
            {% blocktrans trimmed with date=exchange.date|timesince %}
              {{ date }} ago
            {% endblocktrans %}
          </a>
        </li>
      {% empty %}
        <li class="module-item">
          <span class="quiet">
            {% trans 'There is no recent activity' %}
          </span>
        </li>
      {% endfor %}
    </ul>
  </div>

  <form method="post" action="{% url 'projects_integrations_delete' project_slug=project.slug integration_pk=integration.pk %}">
    {% csrf_token %}
    <input type="submit" value="{% trans "Delete webhook" %}">
  </form>
{% endblock %}
